package bc.running.forum.db;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Clob;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import static bc.running.forum.db.Constants.*;

public class PostsTableHandler  implements ITableHandler{

	public PostsTableHandler() {
		File propsFile = new File("tmp.properties");
		if(propsFile.exists()) {
			try {
				props = new Properties();
				props.load(new FileInputStream(propsFile));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public List<String> handleTableData(ResultSet resultSet)  throws  Exception {
		if(resultSet == null) {
			return null;
		}
		
		BufferedWriter postsOut = new BufferedWriter(new OutputStreamWriter
				  (new FileOutputStream(new File("out/" + tables[0] + ".xml")),"UTF8"));
		Utils.writeln(postsOut,"<" + tables[0] + ">");
		
		BufferedWriter postTextOut = new BufferedWriter(new OutputStreamWriter
				  (new FileOutputStream(new File("out/" + tables[1] + ".xml")),"UTF8"));
		Utils.writeln(postTextOut,"<" + tables[1] + ">");

		while(resultSet.next()) {
			Utils.writeln(postsOut,"<row>");
			Utils.writeln(postTextOut,"<row>");
			int replyId = resultSet.getInt(REPLY_ID);
			int topicId = resultSet.getInt(TOPIC_ID);
			int forumId = resultSet.getInt(FORUM_ID);
			int author = resultSet.getInt(R_AUTHOR);
			String _ip = resultSet.getString(R_IP);
			String ip = Utils.encodeIP(_ip);
			Clob _clob = resultSet.getClob(R_MESSAGE);
			String message = Utils.getStringFromClob(_clob);
			/*
			List<String> htmlFraments = HTMLUtils.getHTMLFraments(message);
			if(!htmlFraments.isEmpty()) {
				System.out.println("--" + replyId + " --");
				System.out.println(message);
				System.out.println();
				for (String fragment : htmlFraments) {
					System.out.println(fragment);
				}			
			}
			*/
	
			String _date = resultSet.getString(R_DATE);
			int date = Utils.convertDate(_date);
			
			buildPHPBBPostsRecord(postsOut,replyId,topicId,forumId,author,ip,date);
			buildPHPBBPostsTextRecord(postTextOut,message,replyId,topicId);
			Utils.writeln(postsOut,"</row>");
			Utils.writeln(postTextOut,"</row>");
		}
		Utils.writeln(postsOut,"</" + tables[0] + ">");
		postsOut.close();
		Utils.writeln(postTextOut,"</" + tables[1] + ">");
		postTextOut.close();
		
		return Arrays.asList(tables);
	}
	
	//http://www.phpbbdoctor.com/doc_columns.php?id=10
	private void buildPHPBBPostsTextRecord(BufferedWriter postTextOut,
		String message, int replyId, int topicId) throws Exception {
		String tmp = String.format(Utils.FIELD_TEMPLATE, "post_text",Utils.xmlEscaping(message));
		Utils.writeln(postTextOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "post_id",replyId);
		Utils.writeln(postTextOut, tmp);
		final String subject  =  getPostSubject(topicId);
		tmp = String.format(Utils.FIELD_TEMPLATE, "post_subject",subject);
		Utils.writeln(postTextOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "bbcode_uid","");
		Utils.writeln(postTextOut, tmp);

	}
	private String getPostSubject(int replyId) {
		if(props != null) {
			String tmp = new Integer(replyId).toString();
			return props.getProperty(tmp);
			
		} else {
			return null;
		}
	}

	//http://www.phpbbdoctor.com/doc_columns.php?id=9
	private void buildPHPBBPostsRecord(BufferedWriter postsOut, int replyId,
			int topicId, int forumId, int author, String ip, int date) throws Exception {
		String tmp = String.format(Utils.FIELD_TEMPLATE, "post_id",replyId);
		Utils.writeln(postsOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "topic_id",topicId);
		Utils.writeln(postsOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "forum_id",forumId);
		Utils.writeln(postsOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "poster_id",author);
		Utils.writeln(postsOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "poster_ip",ip);
		Utils.writeln(postsOut, tmp);
		tmp = String.format(Utils.FIELD_TEMPLATE, "post_time",date); 
		Utils.writeln(postsOut, tmp);
		String [] zeroFieldsNames =  new String [] {"enable_html","enable_sig","post_edit_count"};
		for (String fieldName : zeroFieldsNames) {
			tmp = String.format(Utils.FIELD_TEMPLATE,fieldName,0);
			Utils.writeln(postsOut, tmp);
		}
		String [] oneFieldsNames =  new String [] {"enable_bbcode","enable_smilies"};
		for (String fieldName : oneFieldsNames) {
			tmp = String.format(Utils.FIELD_TEMPLATE,fieldName,1);
			Utils.writeln(postsOut, tmp);
		}
		String [] nullFieldsNames =  new String [] {"post_username","post_edit_time"};
		for (String fieldName : nullFieldsNames) {
			tmp = String.format(Utils.FIELD_TEMPLATE,fieldName,null);
			Utils.writeln(postsOut, tmp);
		}
	}
	private Properties props = null;
	private static final String [] tables = new String []{"_phpbb_posts","_phpbb_posts_text"};

}
